package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type DeviceValueExTaskEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	TaskName string // 模板名称

	TaskParam map[string]interface{} // 模板参数
}

type deviceValueExTaskPo struct {
	Id         int64                  `json:"id" db:"id"`                  // 对象id
	CreateTime int64                  `json:"createTime" db:"create_time"` // 创建时间
	UpdateTime int64                  `json:"updateTime" db:"update_time"` // 更新时间
	TaskName   string                 `json:"taskName" db:"task_name"`     // 设备名称
	TaskParam  map[string]interface{} `json:"taskParam" db:"task_param"`   // 配置参数
}

func (e *DeviceValueExTaskEntity) Type() string {
	return "DeviceValueExTaskEntity"
}

func (e *DeviceValueExTaskEntity) TableName() string {
	return "tb_device_value_task"
}

func (e *DeviceValueExTaskEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *DeviceValueExTaskEntity) Instance() *DeviceValueExTaskEntity {
	return &DeviceValueExTaskEntity{
		BaseEntity: new(commEntity.BaseEntity),
		TaskParam:  make(map[string]interface{}),
	}
}

func (e *DeviceValueExTaskEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *DeviceValueExTaskEntity) NewPo() interface{} {
	return &deviceValueExTaskPo{
		TaskParam: make(map[string]interface{}),
	}
}

func (e *DeviceValueExTaskEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *DeviceValueExTaskEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.TaskName)
	return p
}

func (e *DeviceValueExTaskEntity) BuildByJson(jsn *string) error {
	var po deviceValueExTaskPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *DeviceValueExTaskEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *DeviceValueExTaskEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*deviceValueExTaskPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.TaskParam = po.TaskParam
	ptr.TaskName = po.TaskName
}

func (e *DeviceValueExTaskEntity) BuildToPo() interface{} {
	ptr := e
	po := deviceValueExTaskPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.TaskParam = ptr.TaskParam
	po.TaskName = ptr.TaskName

	return &po
}
